home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / uemlsrc / misc.c < prev    next >
C/C++ Source or Header  |  1987-08-24  |  16KB  |  573 lines

  1. /* misc.c  miscellaneous commands, some macros and mode commands */
  2.  
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include <osbind.h>
  6. #include "ed.h"
  7.  
  8. typedef short boolean;
  9. static int count = 1;
  10. char *modes[] = {"Fundamental","Wrap","C"};
  11.  
  12. /* MINDNL : META command Indent subsequent newline same as the present
  13.  * one.  Bound to M-J.
  14.  */
  15.  
  16. mindnl(f, n)
  17. register int f, n;
  18. {
  19.         curwp->w_doto = llength(curwp->w_dotp);
  20.         return(indent(f, n));
  21. }
  22.  
  23. /* MDELELN : CTRL command Delete entire line from beginning. Bound to ^K.
  24.  */
  25.  
  26. mdeleln(f, n)
  27. register int f, n;
  28. {
  29.         curwp->w_doto = 0;
  30.         return(kill(NULL, 1));
  31. }
  32.  
  33. /* MDELWLN : META command Delete entire line including NL. Bound to M-^K.
  34.  */
  35.  
  36. mdelwln(f, n)
  37. register int f, n;
  38. {
  39.         curwp->w_doto = 0;
  40.         return(kill(TRUE, 1));
  41. }
  42.  
  43. /* MDELIND : META command Delete beginning indentation on line. Bound to M-\.
  44.  */
  45.  
  46. mdelind(f, n)
  47. register f, n;
  48. {
  49.         register int odo;
  50.  
  51.         f = llength(curwp->w_dotp);
  52.         odo = curwp->w_doto;
  53.  
  54.         curwp->w_doto = 0;
  55.         while ((n = lgetc(curwp->w_dotp, curwp->w_doto)) <= ' '
  56.                 && curwp->w_doto != llength(curwp->w_dotp))
  57.                 if (forwdel(NULL, 1) == FALSE)
  58.                         return(FALSE);
  59.         if ((n = llength(curwp->w_dotp)) == NULL)
  60.                 curwp->w_doto = n;
  61.         else {
  62.                 if (odo == NULL)
  63.                         curwp->w_doto = odo;
  64.                 else
  65.                         curwp->w_doto = odo - (f - n);
  66.         }
  67.         lchange(WFEDIT);
  68.         return(TRUE);
  69. }
  70.  
  71. /* CLOWSP Meta Command  Close up white space from point forward.
  72.  * Bound to M-^O.
  73.  */
  74.  
  75. clowsp(f, n)
  76. register int f, n;
  77. {
  78.         while ((n = lgetc(curwp->w_dotp, curwp->w_doto)) <= ' '
  79.                 || curwp->w_doto == llength(curwp->w_dotp))
  80.                 if(forwdel(NULL, 1) == FALSE)
  81.                         return(FALSE);
  82.         return(linsert(1, ' '));
  83. }
  84.  
  85. /* MARKPAR : Meta Command Mark paragraph for moving or deleting.  Bound to
  86.  * M-H.
  87.  */
  88.  
  89. markpar(f, n)
  90. register int f, n;
  91. {
  92.         goteop(NULL, n);
  93.         setmark(NULL, 1);
  94.         gotbop(NULL, n);
  95.         return(TRUE);
  96. }
  97.  
  98. /* KILLSENT : Meta command  Kills sentence forward.  Bound to M-K.
  99.  */
  100.  
  101. killsent(f, n)
  102. register int f, n;
  103. {
  104.         setmark(NULL,1);
  105.         forwsent(f, n);
  106.         return(killregion(NULL, 1));
  107.  
  108. }
  109.  
  110. /* TGLCASE : Meta command  Toggles case of letter at point.  Bound to M-^P.
  111.  */
  112.  
  113. tglcase(f, n)
  114. register int f, n;
  115. {
  116.         register int c;
  117.  
  118.         if (n < 0)
  119.                 return(FALSE);
  120.         while(n--)
  121.                 {
  122.                 c = lgetc(curwp->w_dotp, curwp->w_doto);
  123.                 if(islower(c))
  124.                         c = toupper(c);
  125.                 else
  126.                         c = tolower(c);
  127.  
  128.                 lputc(curwp->w_dotp, curwp->w_doto, c);
  129.                 lchange(WFEDIT);
  130.                 if(forwchar(FALSE, 1) == FALSE)
  131.                         return(FALSE);
  132.                 }
  133.         return(TRUE);
  134. }
  135.  
  136. /* GRTW eXtended command  Globally removes trailing white space.  Bound to
  137.  * ^X-\.
  138.  */
  139.  
  140. grtw(f, n)
  141. register int f, n;
  142. {
  143.  
  144.         register int dlo, d;
  145.         register LINE *dlp;
  146.  
  147.         dlp = curwp->w_dotp;
  148.         dlo = curwp->w_doto;
  149.  
  150.         gotobob(NULL, 1);
  151.  
  152.         while((d = ltrw(FALSE, 1)) != EOF)
  153.                 forwline(NULL, 1);
  154.  
  155.         if (dlo > llength(dlp))
  156.                 dlo = llength(dlp);
  157.  
  158.         curwp->w_dotp = dlp;
  159.         curwp->w_doto = dlo;
  160.         curwp->w_flag |= WFHARD;
  161.         return(TRUE);
  162. }
  163.  
  164. /* LTRW internal function delete trailing white space on current line.
  165.  * called by grtw() and fillpar().  Any arguments are ignored.  Returns
  166.  * length of line or EOF at end of buffer.
  167.  */
  168.  
  169. ltrw(f, n)
  170. register int f, n;
  171. {
  172.         register int c, d;
  173.  
  174.         if(curwp->w_dotp == curbp->b_linep)
  175.                 return(EOF);
  176.         d = llength(curwp->w_dotp);
  177.         curwp->w_doto = d;
  178.  
  179.         if (d == NULL)
  180.                 return(NULL);
  181.         while (--d >= 0)
  182.                 {
  183.                 backchar(FALSE, 1);
  184.                 c = lgetc(curwp->w_dotp, curwp->w_doto);
  185.                 if(c != ' ' && c != '\t')
  186.                         break;
  187.                 if(ldelete(1, NULL) == FALSE)
  188.                         return(EOF);
  189.                 }
  190.         return(llength(curwp->w_dotp));
  191. }
  192.  
  193. /* TWADDLE Meta command  More at twiddle.  Transpose two words on a line.
  194.  * Bound to M-T.
  195.  */
  196.  
  197. twaddle(f, n)
  198. register int f, n;
  199. {
  200.         boolean flag = FALSE;
  201.         boolean punct= FALSE;
  202.         register int c;
  203.         char word[20];
  204.  
  205.         n = 0;
  206.  
  207.         if(backchar(FALSE, 1) == FALSE)
  208.                 return (FALSE);
  209.         c = lgetc(curwp->w_dotp, curwp->w_doto);
  210.         if(ispunct(c))
  211.                 {
  212.                 punct = c;
  213.                 if (forwdel(NULL, 1) == FALSE)
  214.                         return (FALSE);
  215.                 }
  216.         else if (forwchar(FALSE, 1) == FALSE)
  217.                         return (FALSE);
  218.  
  219.         c = lgetc(curwp->w_dotp, curwp->w_doto);
  220.         if (isspace(c))
  221.                 flag = c;
  222.         else if (ispunct(c))
  223.                 flag = c;
  224.         else
  225.                 word[n++] = c;
  226.         if (ldelete(1, NULL) == FALSE)
  227.                 return (FALSE);
  228.  
  229.         while(c = lgetc(curwp->w_dotp, curwp->w_doto))
  230.                 {
  231.                 if (curwp->w_doto == llength(curwp->w_dotp))
  232.                         break;
  233.                 if (isalnum(c))
  234.                         {
  235.                         word[n++] = c;
  236.                         if (forwdel(NULL, 1) == FALSE)
  237.                                 return (FALSE);
  238.                         }
  239.                 else
  240.                         break;
  241.                 }
  242.         if(backword(NULL, 1) == FALSE)
  243.                 return (FALSE);
  244.         c = 0;
  245.         while(n--)
  246.                 if (linsert(1, word[c++]) == FALSE)
  247.                         return (FALSE);
  248.         if (punct)
  249.                 if (linsert(1, punct) == FALSE)
  250.                         return (FALSE);
  251.         if (flag)
  252.                 if (linsert(1, flag) == FALSE)
  253.                         return (FALSE);
  254.         return(TRUE);
  255. }
  256.  
  257. /* MRFLUSH META Command right flush current line.  Bound to M-^R */
  258.  
  259. mrflush(f, n)
  260. register int f, n;
  261. {
  262.         register short p;       /* where we are in line */
  263.         register short l;       /* length of line */
  264.         register short o;       /* offset for new location */
  265.  
  266.         p = curwp->w_doto;
  267.         if((l = llength(curwp->w_dotp)) == NULL)
  268.                 return(FALSE);
  269.         if (fillcol)
  270.                 f = fillcol;
  271.         else    f = 80;
  272.         if((o = f - l) < 0)
  273.                 return(FALSE);
  274.         curwp->w_doto = 0;
  275.         linsert(o, ' ');
  276.         forwchar(NULL, p);
  277.         return(TRUE);
  278. }
  279.  
  280. /* MCENTER META Command center current line.  Bound to M-^C */
  281.  
  282. mcenter(f, n)
  283. register int f, n;
  284. {
  285.         register short p;       /* where we are in line */
  286.         register short l;       /* length of line */
  287.         register short o;       /* offset for new location */
  288.  
  289.         p = curwp->w_doto;
  290.         if((l = llength(curwp->w_dotp)) == NULL)
  291.                 return(FALSE);
  292.         if (fillcol)
  293.                 f = fillcol;
  294.         else    f = 80;
  295.         if((o = (f - l)/2) < 0)
  296.                 return(FALSE);
  297.         curwp->w_doto = 0;
  298.         linsert(o, ' ');
  299.         forwchar(NULL, p);
  300.         return(TRUE);
  301. }
  302.  
  303. /* SETMODE Extended Command.  Only one mode at a time.  Bound to ^X-M.
  304.  */
  305.  
  306. setmode(f, n)
  307. register int f, n;
  308. {
  309.         char mymode[12];
  310.         register char *ptr;
  311.         extern char *index();
  312.         register BUFFER *bp;
  313.         register WINDOW *wp;
  314.  
  315.         if((f=mlreply("New mode: ",mymode,12)) != TRUE)
  316.                 return(f);
  317.         /* don't fail because of stray spaces */
  318.         if ((ptr=index(mymode,' '))!=NULL)
  319.                 *ptr = '\0';
  320.         else if ((ptr=index(mymode,'\t'))!=NULL)
  321.                 *ptr = '\0';
  322.         n = strlen(mymode);
  323.         /* adjust case */
  324.         for (f=0;f<=n;f++)
  325.                 if (f == 0)
  326.                         mymode[f] = toupper(mymode[f]);
  327.                 else
  328.                         mymode[f] = tolower(mymode[f]);
  329.         for (f=0;f<=NUMMODES-1;f++)
  330.                 {
  331.                 if (strncmp(modes[f],mymode,12)==0)
  332.                         {
  333.                         switch(f)
  334.                                 {
  335.                                 case 0:
  336.                                         curbp->b_bmode &= ~BMWRAP;
  337.                                         curbp->b_bmode &= ~BMCMODE;
  338.                                         curbp->b_bmode |= BMNWRAP;
  339.                                         break;
  340.                                 case 1:
  341.                                         curbp->b_bmode &= ~BMNWRAP;
  342.                                         curbp->b_bmode &= ~BMCMODE;
  343.                                         curbp->b_bmode |= BMWRAP;
  344.                                         break;
  345.                                 case 2:
  346.                                         curbp->b_bmode &= ~BMNWRAP;
  347.                                         curbp->b_bmode &= ~BMWRAP;
  348.                                         curbp->b_bmode |= BMCMODE;
  349.                                         break;
  350.                                 }
  351.                         upmode();
  352.                         return(TRUE);
  353.                         }
  354.                 }
  355.                 mlwrite("No match");
  356.                 return(FALSE);
  357. }
  358.  
  359. /* SGLMODE META-Command Set the flag glmode.  The flag is used only by
  360.  * bfind and this command.  Call this after reading in a new color file
  361.  * and answer "color" to the mode question to install new colors.
  362.  */
  363.  
  364. sglmode(f, n)
  365. int f, n;
  366. {
  367.         char mymode[12];
  368.         register char *ptr;
  369.         extern char *index();
  370.         register BUFFER *bp;
  371.         register WINDOW *wp;
  372.  
  373.         if((f=mlreply("New global mode: ",mymode,12)) != TRUE)
  374.                 return(f);
  375.         /* don't fail because of stray spaces */
  376.         if ((ptr=index(mymode,' '))!=NULL)
  377.                 *ptr = '\0';
  378.         else if ((ptr=index(mymode,'\t'))!=NULL)
  379.                 *ptr = '\0';
  380.         n = strlen(mymode);
  381.         /* adjust case */
  382.         for (f=0;f<=n;f++)
  383.                 if (f == 0)
  384.                         mymode[f] = toupper(mymode[f]);
  385.                 else
  386.                         mymode[f] = tolower(mymode[f]);
  387.         if (strcmp(mymode,"Color")==NULL)
  388.                 {
  389.                 ttopen();
  390.                 return(TRUE);
  391.                 }
  392.         for (f=0;f<=NUMMODES-1;f++)
  393.                 {
  394.                 if (strncmp(modes[f],mymode,12)==0)
  395.                         {
  396.                         switch(f)
  397.                                 {
  398.                                 case 0:
  399.                                         glmode &= ~BMWRAP;
  400.                                         glmode &= ~BMCMODE;
  401.                                         glmode |= BMNWRAP;
  402.                                         break;
  403.                                 case 1:
  404.                                         glmode &= ~BMNWRAP;
  405.                                         glmode &= ~BMCMODE;
  406.                                         glmode |= BMWRAP;
  407.                                         break;
  408.                                 case 2:
  409.                                         glmode &= ~BMNWRAP;
  410.                                         glmode &= ~BMWRAP;
  411.                                         glmode |= BMCMODE;
  412.                                         break;
  413.                                 }
  414.                         return(TRUE);
  415.                         }
  416.                 }
  417.                 mlwrite("No match");
  418.                 return(FALSE);
  419. }
  420.  
  421. /* RETTPA META-Command returns current memory use and available buffer
  422.  * to msgline.  Bound to M-@.
  423.  */
  424.  
  425. rettpa(f, n)
  426. int f, n;
  427. #if ST
  428. {
  429.         register long low, u_buf;
  430.         unsigned long tpa, stkadd;
  431.         extern long progend;
  432.         extern unsigned long getmem();
  433.         extern long sbrk();
  434.  
  435.         low = sbrk(0);
  436.         stkadd = getmem();
  437.         u_buf = low - progend;
  438.         tpa = stkadd - low;
  439.         mlwrite("Used:%D bytes Heap address:%D Stack address:%lu TPA:%lu bytes",
  440.         u_buf,low,stkadd,tpa);
  441.         return(TRUE);
  442. }
  443. #else
  444. {
  445.         return(FALSE);
  446. }
  447. #endif
  448.  
  449. /* GOLINE  Meta command go to argument line in text.  Bound to M-G.
  450.  */
  451.  
  452. goline(f, n)
  453. register int f, n;
  454. {
  455.         return(gotobob(f, n) && forwline(f, n));
  456. }
  457.  
  458. /* ENUMERATE  Meta Command  Based on value of n, initialize or increment
  459.  * a counter variable.  If n < NULL, simply write the value on the mode
  460.  * line.  Bound to M-CTRL-N.
  461.  */
  462.  
  463. enumerate(f, n)
  464. register int f, n;
  465. {
  466.         char num[5+1];
  467.  
  468.         if (n == -1)    /* request value of count */
  469.                 mlwrite("Counter = %d",count);
  470.         else if (n == TRUE)     /* increment counter */
  471.                 count++;
  472.         else if (n == NULL)
  473.                 {
  474.                 ltoa(num, 5, (long)count++); /* make it a string */
  475.                 putlin(num);    /* put count in doc */
  476.                 putlin(". ");           /* do some formatting */
  477.                 }
  478.         else if (n > 1)                 /* n > 1.  Set counter to arg value */
  479.                 {
  480.                 count = n;
  481.                 enumerate(NULL,-1);
  482.                 }
  483.         else if (n < -1)        /* Request to reset counter */
  484.                 count = 1;
  485.         return(TRUE);
  486. }
  487.  
  488. /* GOSPELL  eXtended command goto next spell mark to correct spelling
  489.  * mistakes.  Bound to X-S.
  490.  */
  491.  
  492. gospell(f, n)
  493. register int f, n;
  494. {
  495.  
  496.         while(TRUE)
  497.                 {
  498.                 if (curwp->w_dotp == curbp->b_linep)
  499.                         {
  500.                         mlwrite("No spell marks found");
  501.                         return(FALSE);
  502.                         }
  503.                 f = lgetc(curwp->w_dotp, curwp->w_doto);
  504.                 if (f == 0x93)
  505.                         {
  506.                         if (forwdel(NULL, TRUE) == FALSE)
  507.                                 return (FALSE);
  508.                         mlwrite("Correction mark found");
  509.                         return(TRUE);
  510.                         }
  511.                 forwchar(NULL,TRUE);
  512.                 }
  513. }
  514.  
  515. /* showtime print current time on mode line.  If n > 1 insert in document.
  516.  */
  517. showtime(f,n)
  518. register int f, n;
  519. {
  520.         register int time;
  521.         register int hours, minutes, seconds;
  522.         char mortem[3]; /* am or pm */
  523.         char timeline[20];
  524.  
  525.         time  = Tgettime();
  526.         seconds = (time&31)*2;
  527.         minutes = (time>>5)&63;
  528.         hours   = (time>>11)&31;
  529.         if (hours >= 12)
  530.                 strcpy(mortem, "pm");
  531.         else
  532.                 strcpy(mortem, "am");
  533.         if (hours > 12)
  534.                 hours-=12;
  535.         if (hours == 0)
  536.                 hours = 12;
  537.         sprintf(timeline,"Time: %d:%02d:%02d %s",hours,minutes,seconds,mortem);
  538.         if (n <= 1)
  539.                 mlwrite(timeline);
  540.         else if (n == HUGE)
  541.                 mtwrite(timeline);
  542.         else
  543.                 putlin(timeline);
  544.         return(TRUE);
  545. }
  546.  
  547. /* MDONCOM  do named command.  Bound to M-X.
  548.  */
  549. mdoncom(f, n)
  550. register int f, n;
  551. {
  552.         char fname[16];         /* function name */
  553.         register char *ptr;
  554.         extern char *index();
  555.         extern short getktpcode();
  556.         register short c;
  557.  
  558.         mlreply("Function: ",fname,15);
  559.         /* don't fail because of stray spaces */
  560.         if ((ptr=index(fname,' '))!=NULL)
  561.                 *ptr = '\0';
  562.         else if ((ptr=index(fname,'\t'))!=NULL)
  563.                 *ptr = '\0';
  564.         if ((c=getktpcode(fname))!=NULL)
  565.                 c = (short)execute(c,f,n);
  566.         else
  567.                 {
  568.                 mlwrite("No match!");
  569.                 (*term.t_beep)();
  570.                 }
  571.         return((int)c);
  572. }
  573.